﻿sampler2D _MainTex : register(S0);
float2 _MainTex_TexelSize : register(C0) = float2(0.002, 0.001);
float _OriginAlphaThreshold : register(C1) = 0.8;
float4 _EdgeColor : register(C2) = float4(1, 0, 0, 1);
float _EdgeDampRate : register(C3) = 2;
float _EdgeAlphaThreshold : register(C4) = 0.5;
 
float _OuterGlowWidth : register(C5) = 1;

float _InnerGlowWidth : register(C6) = 0.1;
float4 _InnerGlowColor : register(C7) = float4(0, 1, 0, 1);
int _InnerGlowAccuracy : register(C8) = int(9);
float _GlowAlphaSumThreshold : register(C9) = 0.05;
float _InnerGlowLerpRate : register(C10) = 0.4;
struct v2f
{
    float4 vertex : SV_POSITION;
    float2 uv[9] : TEXCOORD0;
};
 
float CalculateAlphaSumAround(v2f i)
{
    float texAlpha;
    float alphaSum = 0;
    for (int it = 0; it < 9; it++)
    {
        texAlpha = tex2D(_MainTex, i.uv[it]).a;
        alphaSum += texAlpha;
    }
 
    return alphaSum;
}
float CalculateCircleSumAlpha(float2 orign, float radiu, int time)
{
    float sum = 0;
    float perAngle = 360 / time;
    for (int i = 0; i < time; i++)
    {
        float2 newUV = orign + radiu * float2(cos(perAngle * i), sin(perAngle * i));
        sum += tex2D(_MainTex, newUV).a;
    }
 
    return sum;
}

v2f vert(float2 uv)
{
    v2f o;
 
    o.uv[0] = uv + _MainTex_TexelSize.xy * half2(-1, -1);
    o.uv[1] = uv + _MainTex_TexelSize.xy * half2(0, -1);
    o.uv[2] = uv + _MainTex_TexelSize.xy * half2(1, -1);
    o.uv[3] = uv + _MainTex_TexelSize.xy * half2(-1, 0);
    o.uv[4] = uv + _MainTex_TexelSize.xy * half2(0, 0);
    o.uv[5] = uv + _MainTex_TexelSize.xy * half2(1, 0);
    o.uv[6] = uv + _MainTex_TexelSize.xy * half2(-1, 1);
    o.uv[7] = uv + _MainTex_TexelSize.xy * half2(0, 1);
    o.uv[8] = uv + _MainTex_TexelSize.xy * half2(1, 1);
    return o;
}
 
float4 main(float2 uv : TEXCOORD) : COLOR
{
    v2f i = vert(uv);
    float4 innerGlow = float4(0, 0, 0, 0);
    float4 outline = float4(0, 0, 0, 0);
    float4 orignColor = tex2D(_MainTex, i.uv[4]);
    if (_OuterGlowWidth > 0)
    {
        float alphaSum = CalculateAlphaSumAround(i);
        bool isNeedShow = alphaSum > _EdgeAlphaThreshold;
        float damp = saturate((alphaSum - _EdgeAlphaThreshold) * _EdgeDampRate);
        float isOrigon = orignColor.a > _OriginAlphaThreshold;
        float3 finalColor = lerp(_EdgeColor.rgb, float3(0, 0, 0), isOrigon);
        int needshow = 0;
        if (isNeedShow)
        {
            needshow = 1;
        }
        float finalAlpha = needshow * damp * (1 - isOrigon);
        outline = float4(finalColor.r, finalColor.g, finalColor.b, finalAlpha);
        
    }
    if (_InnerGlowWidth > 0)
    {
        float alphaCircleSum = CalculateCircleSumAlpha(i.uv[4], _InnerGlowWidth, 15) / 15;
        float innerColorAlpha = 0;
        innerColorAlpha = 1 - saturate(alphaCircleSum - _GlowAlphaSumThreshold) / (1 - _GlowAlphaSumThreshold);
  
        if (orignColor.a <= _OriginAlphaThreshold)
        {
            innerColorAlpha = 0;
        }
 
        float3 innerColor = _InnerGlowColor.rgb * innerColorAlpha;
        innerGlow = float4(innerColor.rgb, innerColorAlpha);
    //return innerGlow;
    }
  
    if (_OuterGlowWidth > 0)
    {
        float outlineAlphaDiscard = orignColor.a > _OriginAlphaThreshold;
        orignColor = outlineAlphaDiscard * orignColor;
    }
    return lerp(orignColor, innerGlow * 2, _InnerGlowLerpRate * innerGlow.a) + outline;


     //return tex2D(_MainTex, i.uv[4]) + innerGlow + outline;
    //return lerp(orignColor, innerGlow * 2, _InnerGlowLerpRate * innerGlow.a) + outline;
}